package com.kly.cashmall.module.home.home_tab.tab_container;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;

import com.kly.cashmall.R;
import com.kly.cashmall.utils.ViewHolder;

/**
 * @author
 * @date 2017/6/26
 */
public class BottomTabContainerView extends LinearLayout implements TabContainerView {

    private LinearLayout tabContainer;
    private FrameLayout fragmentFrame;

    private OnItemClickListener itemClickListener;

    public BottomTabContainerView(Context context) {
        this(context, null);
    }

    public BottomTabContainerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BottomTabContainerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        setContentView(R.layout.layout_tab_bottom_tab_container_view);
    }

    public void setContentView(@LayoutRes int layoutId) {
        LayoutInflater.from(getContext()).inflate(layoutId, this, true);
        fragmentFrame = (FrameLayout) findViewById(R.id.tab_fragment_container);
        tabContainer = (LinearLayout) findViewById(R.id.tab_container);
    }

    @Override
    public int getFragmentContainerId() {
        return fragmentFrame.getId();
    }

    @Override
    public void addTabChild(View tabChild) {
        LayoutParams params =
                new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f);
        tabContainer.addView(tabChild, params);

        tabChild.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    if (itemClickListener.onItemClick(tabContainer.indexOfChild(v), v)) {
                        setSelectedItem(tabContainer.indexOfChild(v));
                    }
                }
            }
        });

    }

    @Override
    public int getSize() {
        return tabContainer.getChildCount();
    }

    @Override
    public void removeTabContainer() {
        tabContainer.removeAllViews();
    }

    /**
     * 选中项
     */
    @Override
    public void setSelectedItem(int pos) {
        for (int i = 0; i < tabContainer.getChildCount(); i++) {
            View view = tabContainer.getChildAt(i);
            ViewHolder holder = ViewHolder.getHolder(view);
            TextView textView = holder.get(R.id.tab_text);
            if (pos == i){
                textView.setTextColor(getContext().getResources().getColor(R.color.color_955B00));
            }else {
                textView.setTextColor(getContext().getResources().getColor(R.color.color_999999));
            }
            if (view != null) {
                view.setEnabled(pos != i);
                view.setSelected(pos == i);
            }
        }
    }

    @Override
    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
}
